import numpy as np

# 规则树中序遍历顺序
# 19 编码
# in_order = ["ship", "craft", "airplane", "vehicle", "automobile", "motor_vehicle", "truck", "whole", "frog", "vertebrate", "bird", "chordate", "cat", "carnivore", "dog", "placental", "ungulate", "deer", "horse"]
# 9 编码
in_order = ["craft", "vehicle", "motor_vehicle", "whole", "vertebrate", "chordate", "carnivore", "placental", "ungulate"]
class_2_num = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
num_2_class = {str(class_2_num[k]): k for k in class_2_num}
leaf_fathers = {
    "ship" : ["craft", "vehicle", "whole"],
    "airplane" : ["craft", "vehicle", "whole"],
    "automobile" : ["motor_vehicle", "vehicle", "whole"],
    "truck" : ["motor_vehicle", "vehicle", "whole"],
    "frog" : ["vertebrate", "chordate", "whole"],
    "bird" : ["vertebrate", "chordate", "whole"],
    "cat" : ["carnivore", "placental", "chordate", "whole"],
    "dog" : ["carnivore", "placental", "chordate", "whole"],
    "deer" : ["ungulate", "placental", "chordate", "whole"],
    "horse" : ["ungulate", "placental", "chordate", "whole"],
    }

rule = {}
for k in leaf_fathers:
    # print(f"{k}-{dic[k]}")
    t = [in_order.index(x) for x in leaf_fathers[k]]

    # 采用 19 编码，自身作为元素之一
    # t.append(in_order.index(k))

    if len(t) != len(leaf_fathers[k]):
        raise print(f"error-{k}")
        break
    vec = np.zeros(len(in_order))
    vec[t] = 1
    rule[k] = vec
# print(rule)

def get_rule(num):
    return rule[num_class[num]]


if __name__ == "__main__":
    pass